const Card = function (ifm, size = null, picShow = false, shadow = false) {
    if (ifm.attack == -2 || ifm.attack =='-2') {
        ifm.attack='?'
    }
    if (ifm.defend == -2 || ifm.defend == '-2') {
        ifm.defend = '?'
    }
    this.ifm = ifm;
    this.attribute_ = this._attribute_;
    this.desc_ = this._desc_;
    this.race_ = this._race_;
    this.link = this._link_;
    this.size = size;
    this.random = Math.random(2) - 1;
    this.scale = this._scale_;
    this.picShow=picShow;
    this.shadow=shadow;
}

Card.prototype = {
    get trans() {
        return {
            attribute: {
                light: "光",
                dark: "暗",
                wind: "风",
                ground: "地",
                water: "水",
                fire: "炎",
                god: "神"
            },
            type: {
                monster: "怪兽", magic: "魔法", tragic: "陷阱", tc: "通常", xg: "效果", ys: "仪式", rh: "融合", tt: "同调", cl: "超量", lj: "连接", to: "衍生物", tz: "调整", lh: "灵魂", ec: "二重", tm: "同盟", fz: "反转", kt: "卡通", sm: "特殊召唤", yx: "永续", sg: "速攻", zb: "装备", ys: "仪式", cd: "场地", fj: "反击"
            }
        }
    },
    get _desc_() {
        const desc = this.ifm.desc;
        if (this.ifm.type3 == 'lb') {
            var lb_num = parseInt(desc.substr(1));
            let temp = desc.split("→")[1];
            var lb_desc = temp.split("【")[0].replace("\r", "").replace("\n", "").replace(" ", "");
            var desc_ = temp.split("【")[1].replace("\r", "").replace("\n", "").replace(" ", "");
            this.lb_num = lb_num;
            this.lb_desc = lb_desc;
        }
        else {
            var desc_ = desc.replace("\r", "").replace("\n", "").replace(" ", "");
        }
        return desc_;
    },
    get _link_() {
        if (this.ifm.type2 != "lj") {
            this.link_num = 0;
            return [false, false, false, false, false, false, false, false, false];
        }
        let link = this.ifm.defend;
        let link_ = link.toString(2);
        if (link_.length>9) {
            link_=link_.substr(-9);
        }
        let num = 0;
        let res = [false, false, false, false, false, false, false, false, false];
        for (let i = 0; i < link_.length; i++) {
            let s = link_.substr(i, 1);
            if (parseInt(s)) {
                res[i+9-link_.length] = true;
                num++;
            }
        }
        this.link_num = num;
        return res;
    },
    get _race_() {
        const race = this.ifm.race;
        const trans = this.trans;
        if (this.ifm.type == "monster") {
            var txt = "【" + race;
            if (this.ifm.type2 != "tc" && this.ifm.type2 != "xg") {
                txt += "/" + trans.type[this.ifm.type2];
            }
            if (this.ifm.type3 != "tc" && this.ifm.type3 != null && this.ifm.type3 != "") {
                if (this.ifm.type3 == 'lb') {
                    var type3 = "灵摆";
                }
                else {
                    var type3 = trans.type[this.ifm.type];
                }
                txt += "/" + type3;
            }
            if (this.ifm.type4 != "tc" && this.ifm.type4 != null && this.ifm.type4 != "" && this.ifm.type4 != this.ifm.type3) {
                txt += "/" + trans.type[this.ifm.type4];
            }
            if (this.ifm.type2 != "tc" && this.ifm.type3 != "tc" && this.ifm.type4 != "tc") {
                txt += "/效果】";
            }
            else {
                txt += "/通常】";
            }
            return txt;
        }
        else if (this.ifm.type == "magic") {
            return "【魔法卡】";
        }
        else {
            return "【陷阱卡】";
        }
    },
    get _attribute_() {
        const attribute = this.ifm.attribute;
        const trans = this.trans;
        return trans.attribute[attribute];
    },
    born(size) {
        return new Card(this.ifm, size);
    },
    get _scale_() {
        const name = this.ifm.name;

        var s0 = new RegExp("[a-z]+");
        var s1 = new RegExp("[0-9]+");
        var s2 = new RegExp(/[\u0000-\u00ff]/g);

        let num = 0;
        for (let i = 0; i < name.length; i++) {
            let char = name.substr(i, 1);
            if (s0.test(char) || s1.test(char) || s2.test(char)) {
                num += 0.5;
            }
            else {
                num++;
            }
        }
        if (num < 9) {
            return 1;
        }
        let scale = 8.5 / num;
        return scale;
    }
}

module.exports.Card = Card;